package com.yic.module.device.service.maintenancePlanEntry;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.device.controller.admin.inspectionDeviceEntry.vo.InspectionDeviceEntryExportReqVO;
import com.yic.module.device.controller.admin.maintenanceDeviceEntry.vo.MaintenanceDeviceEntryExportReqVO;
import com.yic.module.device.dal.dataobject.inspectionDeviceEntry.InspectionDeviceEntryDO;
import com.yic.module.device.dal.dataobject.maintenanceDeviceEntry.MaintenanceDeviceEntryDO;
import com.yic.module.device.service.maintenanceDeviceEntry.MaintenanceDeviceEntryService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.yic.module.device.controller.admin.maintenancePlanEntry.vo.*;
import com.yic.module.device.dal.dataobject.maintenancePlanEntry.MaintenancePlanEntryDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.device.convert.maintenancePlanEntry.MaintenancePlanEntryConvert;
import com.yic.module.device.dal.mysql.maintenancePlanEntry.MaintenancePlanEntryMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.device.enums.ErrorCodeConstants.*;

/**
 * 设备保养项目 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class MaintenancePlanEntryServiceImpl implements MaintenancePlanEntryService {

    @Resource
    private MaintenancePlanEntryMapper maintenancePlanEntryMapper;
    @Resource
    private MaintenanceDeviceEntryService maintenanceDeviceEntryService;
    @Resource
    private TransactionTemplate transactionTemplate;

    @Override
    public Long createMaintenancePlanEntry(MaintenancePlanEntryCreateReqVO createReqVO) {
        // 插入
        MaintenancePlanEntryDO maintenancePlanEntry = MaintenancePlanEntryConvert.INSTANCE.convert(createReqVO);
        maintenancePlanEntryMapper.insert(maintenancePlanEntry);
        // 返回
        return maintenancePlanEntry.getId();
    }

    @Override
    public void updateMaintenancePlanEntry(MaintenancePlanEntryUpdateReqVO updateReqVO) {
        // 校验存在
        validateMaintenancePlanEntryExists(updateReqVO.getId());
        // 更新
        MaintenancePlanEntryDO updateObj = MaintenancePlanEntryConvert.INSTANCE.convert(updateReqVO);
        maintenancePlanEntryMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaintenancePlanEntry(Long id,Boolean flag) {
        // 校验存在
        validateMaintenancePlanEntryExists(id);
        transactionTemplate.execute(status -> {
            maintenancePlanEntryMapper.deleteById(id);
            // 删除设备和检查项关联信息
            if (ObjectUtil.isNotNull(flag) && flag.equals(Boolean.TRUE)) {
                MaintenanceDeviceEntryExportReqVO maintenanceDeviceEntryExportReqVO = new MaintenanceDeviceEntryExportReqVO();
                maintenanceDeviceEntryExportReqVO.setEntryId(id);
                List<MaintenanceDeviceEntryDO> maintenanceDeviceEntryList = maintenanceDeviceEntryService.getMaintenanceDeviceEntryList(maintenanceDeviceEntryExportReqVO);
                List<Long> idList = maintenanceDeviceEntryList.stream().map(MaintenanceDeviceEntryDO::getId).collect(Collectors.toList());
                for (Long idT : idList) {
                    maintenanceDeviceEntryService.deleteMaintenanceDeviceEntry(idT);
                }
            }
            return Boolean.TRUE;
        });
    }

    private void validateMaintenancePlanEntryExists(Long id) {
        if (maintenancePlanEntryMapper.selectById(id) == null) {
            throw exception(MAINTENANCE_PLAN_ENTRY_NOT_EXISTS);
        }
    }

    @Override
    public MaintenancePlanEntryDO getMaintenancePlanEntry(Long id) {
        return maintenancePlanEntryMapper.selectById(id);
    }

    @Override
    public List<MaintenancePlanEntryDO> getMaintenancePlanEntryList(Collection<Long> ids) {
        return maintenancePlanEntryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MaintenancePlanEntryDO> getMaintenancePlanEntryPage(MaintenancePlanEntryPageReqVO pageReqVO) {
        return maintenancePlanEntryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MaintenancePlanEntryDO> getMaintenancePlanEntryList(MaintenancePlanEntryExportReqVO exportReqVO) {
        return maintenancePlanEntryMapper.selectList(exportReqVO);
    }

    @Override
    public PageResult<MaintenancePlanEntryDO> getMaintenancePlanEntryNotSelectPage(MaintenancePlanEntryPageReqVO pageVO) {
        MaintenanceDeviceEntryExportReqVO reqVO = new MaintenanceDeviceEntryExportReqVO();
        reqVO.setDeviceId(pageVO.getDeviceId());
        List<MaintenanceDeviceEntryDO> maintenanceDeviceEntryList = maintenanceDeviceEntryService.getMaintenanceDeviceEntryList(reqVO);
        List<Long> entryIdList = maintenanceDeviceEntryList.stream().map(MaintenanceDeviceEntryDO::getEntryId).collect(Collectors.toList());
        return maintenancePlanEntryMapper.getMaintenancePlanEntryNotSelectPage(pageVO,entryIdList);
    }

}
